package it.unisa.indianamas.idm;


import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

import com.mathworks.toolbox.javabuilder.*;

import RadonSOM2.*;
import it.unisa.indianamas.idm.RadonSOMResult;
import it.unige.indianamas.ontology.*;
import it.unige.indianamas.ontology.impl.DefaultCalculated_Classification;
import it.unige.indianamas.ontology.impl.DefaultZigzag_Line;
public class RadonSOM {
	private String query;
	private String mapfilename;
	private String symbolfilename;
	private int size;
	private double theta;

	private ArrayList<RadonSOMResult> results = new ArrayList<RadonSOMResult>();
	private ArrayList<DefaultCalculated_Classification> classifications = new ArrayList<DefaultCalculated_Classification>(); 
	private Properties prop = new Properties();
	
	
	
	public RadonSOM(String query, String mapfilename, String symbolfilename,  int size,
			double theta) {
		super();
		this.query = query;
		this.mapfilename = mapfilename;
		this.symbolfilename = symbolfilename;
		this.size = size;
		this.theta = theta;
		
   	 
    	try {
            //load a properties file
 		prop.load(new FileInputStream("src/main/resources/mapping.properties"));

            //get the property value and print it out
        query = prop.getProperty("query");

 
    	} catch (IOException ex) {
    		ex.printStackTrace();
        }

	}
	
	public ArrayList<DefaultCalculated_Classification> run() throws Exception{
		RadonSOM2.Class1 x = null;
		Object[] result = null;
		
	
			x = new RadonSOM2.Class1();
			Object[] list = new Object[5];
			list[0] = query;
			list[1] = mapfilename;
			list[2] = symbolfilename;
			list[3] = size;
			list[4] = theta;
	
			
			result = x.querybyname(1, list);
			MWCellArray mwCellArray = (MWCellArray) result[0];
			StringTokenizer st2 = new StringTokenizer(mwCellArray.toString(), "\n");
			
			while (st2.hasMoreElements()) {
				StringTokenizer st = new StringTokenizer((String) st2.nextElement(),"'[]");

				st.nextElement();

				String category = (String) st.nextElement();
				st.nextElement();

				double distance = Double.parseDouble((String)st.nextElement());
				DefaultCalculated_Classification classification =  new  DefaultCalculated_Classification();
				//RadonSOMResult resultComparison = new RadonSOMResult(category, distance);
				classification.setAutomatic(true);
				classification.setSource("Radon+SOM");
				classification.setConfidence((int)distance);
				String clazzName = prop.getProperty(category);
				Class clazz = Class.forName(clazzName);
				Constructor co = clazz.getConstructor();
				Object object = co.newInstance();
				classification.setIdentified_classification((Classification) object);

				classifications.add(classification);
			}
		return classifications;
	}
	
	

	
	
	
}
